package com.yanqu.road.dao.impl.activity.ghost;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.ghost.GhostPatrons;
import com.yanqu.road.entity.activity.ghost.GhostUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GhostUserDataDaoImpl extends TempDataObjectDao<GhostUserData> {

    public static String getGhostUserDataSql = "select * from t_u_activity_ghost_user_data where activityId=? and userId=?";

    public GhostUserData getGhostUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getGhostUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                GhostUserData ghostUserData = parse(rs);
                return ghostUserData;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateGhostUserDataSql = "update t_u_activity_ghost_user_data set energy=?,lastRecoveryTime=?,eventType=?,param=?,complete=?,eventTime=?,getDisperse=?,buffLevel=?,score=?,tokenCount=?,attackMap=?,resetTime=?,buyEnergyTimes=?,getCleanDisperse=?,exploreTimes=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(GhostUserData ghostUserData) {
        ghostUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, ghostUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostUserData.getLastRecoveryTime()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostUserData.getEventType()));
        para.put(i++, new DbParameter(Types.VARCHAR, ghostUserData.getParam()));
        para.put(i++, new DbParameter(Types.BOOLEAN, ghostUserData.isComplete()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostUserData.getEventTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, ghostUserData.isGetDisperse()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostUserData.getBuffLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostUserData.getTokenCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(ghostUserData.getAttackMap())));
        para.put(i++, new DbParameter(Types.BIGINT, ghostUserData.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostUserData.getBuyEnergyTimes()));
        para.put(i++, new DbParameter(Types.BOOLEAN, ghostUserData.isGetCleanDisperse()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostUserData.getExploreTimes()));

        para.put(i++, new DbParameter(Types.INTEGER, ghostUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostUserData.getUserId()));
        if (getDb().executeNoneQuery(updateGhostUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertGhostUserDataSql = "insert into t_u_activity_ghost_user_data(activityId,userId,energy,lastRecoveryTime,eventType,param,complete,eventTime,getDisperse,buffLevel,score,tokenCount,attackMap,resetTime,buyEnergyTimes,getCleanDisperse,exploreTimes) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(GhostUserData ghostUserData) {
        ghostUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, ghostUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostUserData.getLastRecoveryTime()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostUserData.getEventType()));
        para.put(i++, new DbParameter(Types.VARCHAR, ghostUserData.getParam()));
        para.put(i++, new DbParameter(Types.BOOLEAN, ghostUserData.isComplete()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostUserData.getEventTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, ghostUserData.isGetDisperse()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostUserData.getBuffLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostUserData.getTokenCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(ghostUserData.getAttackMap())));
        para.put(i++, new DbParameter(Types.BIGINT, ghostUserData.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostUserData.getBuyEnergyTimes()));
        para.put(i++, new DbParameter(Types.BOOLEAN, ghostUserData.isGetCleanDisperse()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostUserData.getExploreTimes()));
        if (getDb().executeNoneQuery(insertGhostUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected GhostUserData parseData(ResultSet rs) throws SQLException {
        GhostUserData ghostUserData = new GhostUserData();
        ghostUserData.setActivityId(rs.getInt("activityId"));
        ghostUserData.setUserId(rs.getLong("userId"));
        ghostUserData.setEnergy(rs.getInt("energy"));
        ghostUserData.setLastRecoveryTime(rs.getLong("lastRecoveryTime"));
        ghostUserData.setEvent(rs.getInt("eventType"), rs.getString("param"), rs.getLong("eventTime"));
        ghostUserData.setComplete(rs.getBoolean("complete"));
        ghostUserData.setGetDisperse(rs.getBoolean("getDisperse"));
        ghostUserData.setBuffLevel(rs.getInt("buffLevel"));
        ghostUserData.setScore(rs.getLong("score"));
        ghostUserData.setTokenCount(rs.getInt("tokenCount"));
        String attackMap = rs.getString("attackMap");
        ghostUserData.setAttackMap(StringUtils.isNullOrEmpty(attackMap) ? new ConcurrentHashMap<>() : JSONObject.parseObject(attackMap, new TypeReference<Map<Integer, GhostPatrons>>() {
        }));
        ghostUserData.setResetTime(rs.getLong("resetTime"));
        ghostUserData.setBuyEnergyTimes(rs.getInt("buyEnergyTimes"));
        ghostUserData.setGetCleanDisperse(rs.getBoolean("getCleanDisperse"));
        ghostUserData.setExploreTimes(rs.getInt("exploreTimes"));
        return ghostUserData;
    }

}
